package com.central.apps.service.impl;

import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.central.app.service.HandlerService;
import com.central.apps.dao.MobilePictureDao;
import com.central.apps.dao.OnlineEditDao;
import com.central.apps.entity.MobilePicture;
import com.central.apps.entity.OnlineEdit;
import com.central.apps.service.MobilePictureService;
import com.central.apps.service.OnlineEditService;
import com.central.common.auth.details.LoginAppUser;
import com.central.common.util.SysUserUtil;
import com.central.common.web.PageResult;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author JHY
 */
@Service
public class MobilePictureServiceImpl extends ServiceImpl<MobilePictureDao, MobilePicture> implements MobilePictureService {

    @Autowired
    private MobilePictureDao mobilePictureDao;
    @Autowired
    private HandlerService handlerService;


    public void insert(List<Map<String,Object>> params){
        if(CollectionUtils.isNotEmpty(params)){

            for (Map<String, Object> param : params) {
                String type = MapUtils.getString(param, "type");
                if("1".equals(type)){
                    //扫码入库存储照片
                    codeScanningWarehousing(param);
                }else if("2".equals(type)){
                    //库存盘点存储照片
                    inventorycount(param);
                }
            }
        }

    }

    public void codeScanningWarehousing(Map<String, Object> parmas) {
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser();
        Long id = loginAppUser.getId();
        String itemnum = org.apache.commons.collections.MapUtils.getString(parmas, "itemnum");//物料编码
        String batchnum = org.apache.commons.collections.MapUtils.getString(parmas, "batchnum");//批次
        String fileid = org.apache.commons.collections.MapUtils.getString(parmas, "fileid");//文件id
        String webUrl = org.apache.commons.collections.MapUtils.getString(parmas, "webUrl");//webUrl
        Integer prtdetaillineid = null;
        List<Map<String, Object>> bySql = handlerService.findBySql("select id from gq_prtdetailline where itemnum ='" + itemnum + "' and batchnum = '" + batchnum +"'");
        if(CollectionUtils.isNotEmpty(bySql)) {
            prtdetaillineid = MapUtils.getInteger(bySql.get(0), "id");
        }



        String[] split = fileid.split(",");
        List<String> fileIdList = Arrays.asList(split);
        for (String s : fileIdList) {
            List<Map<String, Object>> bySql1 = handlerService.findBySql("select filepath from tb_file where fileid ='" + s + "'");
            if(CollectionUtils.isNotEmpty(bySql1)){
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(webUrl);
                Map<String, Object> map = bySql1.get(0);
                String filepath = MapUtils.getString(map, "filepath");
                stringBuilder.append("/file").append(filepath);
                MobilePicture mobilePicture = new MobilePicture();
                mobilePicture.setPrtdetaillineid(prtdetaillineid);
                mobilePicture.setCreatetime(new Date());
                mobilePicture.setUrl(stringBuilder.toString());
                mobilePicture.setCreateuser(id.toString());
                mobilePictureDao.insert(mobilePicture);
            }
        }
    }

    public void inventorycount(Map<String, Object> parmas){
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser();
        Long id = loginAppUser.getId();
        Integer checkid = MapUtils.getInteger(parmas, "checkid");//物料库存盘点这条数据id
        String fileid = org.apache.commons.collections.MapUtils.getString(parmas, "fileid");//文件id
        String webUrl = org.apache.commons.collections.MapUtils.getString(parmas, "webUrl");//webUrl


        String[] split = fileid.split(",");
        List<String> fileIdList = Arrays.asList(split);
        for (String s : fileIdList) {
            List<Map<String, Object>> bySql1 = handlerService.findBySql("select filepath from tb_file where fileid ='" + s + "'");
            if(CollectionUtils.isNotEmpty(bySql1)){
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(webUrl);
                Map<String, Object> map = bySql1.get(0);
                MobilePicture mobilePicture = new MobilePicture();
                String filepath = MapUtils.getString(map, "filepath");
                stringBuilder.append("/file").append(filepath);
                mobilePicture.setCheckid(checkid);
                mobilePicture.setCreatetime(new Date());
                mobilePicture.setUrl(stringBuilder.toString());
                mobilePicture.setCreateuser(id.toString());
                mobilePictureDao.insert(mobilePicture);
            }
        }
    }

    public String queryIdByItemBach(Map<String, Object> params) {
        String itemnum = MapUtils.getString(params, "itemnum");//物料编码
        String batchnum = MapUtils.getString(params, "batchnum");//批次
        String prtdetaillineid = null;
        List<Map<String, Object>> bySql = handlerService.findBySql("select id from gq_prtdetailline where itemnum ='" + itemnum + "' and batchnum = '" + batchnum +"'");
        if(CollectionUtils.isNotEmpty(bySql)){
            prtdetaillineid = MapUtils.getString(bySql.get(0), "id");
        }
        return prtdetaillineid;
    }

    public List<Map<String, Object>> queryPicUrlById(Map<String, Object> params) {
        List<Map<String, Object>> bySql = new ArrayList<>();
        String type = MapUtils.getString(params, "type");
        String id = MapUtils.getString(params, "id");

        if("1".equals(type)){
            //扫码入库
            bySql = handlerService.findBySql("select url from gq_mobilepicture where prtdetaillineid = '" + id + "'");
        }else if("2".equals(type)){
            //库存盘点
            bySql = handlerService.findBySql("select url from gq_mobilepicture where checkid = '" + id + "'");
        }
        return bySql;
    }
}
